package com.xh.jf_blog.controller.admin;

import com.xh.jf_blog.bean.Banner;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/2/1/001
 */
@Controller
@RequestMapping("/admin/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @GetMapping("/list")
    public String list() {
        return "admin/banner/banner-list";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Banner banner) {
        return bannerService.getPage(page, limit, banner);
    }

    @GetMapping("/edit")
    public String edit(Integer id, Model model) {
        if (id != null) {
            model.addAttribute("banner", bannerService.getById(id));
        }
        return "admin/banner/banner-update";
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(Banner banner) {
        bannerService.saveOrUpdate(banner);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable int id) {
        bannerService.deleteById(id);
        return R.ok();
    }
}
